<template>
  <div class="home">
    <div class="header">
      <span>todolist</span>
      <input type="text" v-model="title" @keydown.enter="add" />
    </div>
    <!-- 头部添加部分 -->

    <ul class="con">
      <h3>正在进行 {{ num2 }}</h3>

      <li
        v-for="(item, index) in list"
        :key="index"
        v-show="item.status == false"
      >
        <!-- 循环所有的数据 但是通过v-show 只展示status为false也就是没有完成的任务 -->
        <!-- v-for只能和v-show搭配不能和v-if搭配 -->
        <input type="checkbox" v-model="item.status" />
        <!-- 控制完成未完成的 -->

        <input
          type="text"
          v-model="item.title"
          v-if="flag == index"
          @blur="flag = -1"
        />
        <!-- 修改框 -->
        <span @dblclick="edit(index)" v-else>{{ item.title }}</span>

        <span @click="remove(index)">x</span>
      </li>
    </ul>
    <ul class="con">
      <h3>已经完成 {{ num1 }}</h3>
      <li
        v-for="(item, index) in list"
        :key="index"
        v-show="item.status == true"
      >
        <input type="checkbox" v-model="item.status" />

        <input
          type="text"
          v-model="item.title"
          v-if="flag == index"
          @blur="flag = -1"
        />
        <span @dblclick="edit(index)" v-else>{{ item.title }}</span>
        <span @click="remove(index)">x</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        {
          title: "任务1",
          status: false,
        },
        {
          title: "任务2",
          status: true,
        },
        {
          title: "任务3",
          status: false,
        },
        {
          title: "任务4",
          status: true,
        },
        {
          title: "任务5",
          status: false,
        },
      ],
      title: "",
      flag: -1,
      //flag是为了保存编辑的数据的下标
    };
  },
  mounted() {
    if (localStorage.getItem("todolist")) {
      this.list = JSON.parse(localStorage.getItem("todolist"));
    }
    //进入页面拿到 存的本地存储来用
  },
  methods: {
    remove(index) {
      this.list.splice(index, 1);
    },
    //删除
    add() {
      this.list.push({ title: this.title, status: false });
      this.title = "";
    },
    //添加
    edit(index) {
      this.flag = index;
    },
    //双击修改的时候把下标存下来
  },
  computed: {
    // 计算任务数量
    num1() {
      let arr = this.list.filter((item) => {
        return item.status == true;
      });
      return arr.length;
    },
    //已经完成的数量
    num2() {
      let arr = this.list.filter((item) => {
        return item.status == false;
      });
      return arr.length;
    },
    //未完成的数量
  },

  watch: {
    list: {
      handler(newval) {
        localStorage.setItem("todolist", JSON.stringify(this.list));
      },
      deep: true,
    },
  },
  //监听list 只要一改变就把list存在本地存储中
};
</script>
<style lang="scss" scoped>
.home {
  .header {
    height: 80px;
    background: rgb(65, 63, 63);
    display: flex;
    padding: 0 300px;
    justify-content: space-around;
    align-items: center;
    span {
      font-size: 30px;
      color: #fff;
    }
    input {
      width: 500px;
      height: 50px;
      border: none;
      border-radius: 10px;
    }
  }
  .con {
    width: 1000px;
    margin: 50px auto;
    li {
      width: 100%;
      height: 50px;
      background: #ccc;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        font-size: 30px;
      }
    }
  }
}
</style>
